import SearchSharp from '@@/icons/search'
import { NInput, NIcon, NSpace, NButton } from 'naive-ui'
import { copyValue } from '@/util'
import gradeApi from '@/request/grade'
import api from '@/request/student'
import { ref } from 'vue'
import { createDiscreteApi } from 'naive-ui'
const { message } = createDiscreteApi(['message'])
const createTableColumns = (data) => {
	const name = ref('')
	const reflush = async () => {
		data.tableData.value = await api.getTableData(data.params)
	}
	const slots = {
		suffix: () => <NIcon style="cursor: pointer" size="16" component={SearchSharp} onClick={() => search(name.value)}></NIcon>
	}
	const search = (val) => {
		data.params.pageNo = 1
		data.params.name = val
		reflush()
	}
	const update = id => {
		data.showEditProps.id = id
		data.showEditProps.title = '修改教师'
		data.showEditProps.show = true
	}
	const del = id => {
		api.remove(id, content => {
			reflush()
		})
	}
	return [
		{
			title: '学号',
			key: 'studentNo'
		},
		{
			title: '姓名',
			key: 'name'
		},
		{
			title: '性别',
			key: 'sex',
		},
		{
			title: '手机号',
			key: 'phone',
			ellipsis: true
		},
		{
			title: '电子信箱',
			key: 'email',
			ellipsis: true
		},
		{
			title: '入学年份',
			key: 'year',
		},
		{
			title: '是否在校/岗',
			key: 'inSchool',
		},
		{
			title: () => (<div class="table-title">
				<NInput 
					value={name.value}
					placeholder="请输入要搜索的管理员姓名"
					on-update:value={e => { name.value = e }}
					on-keydown={e => { if (e.keyCode === 13) {
						search(name.value)
					} }}
					v-slots={slots}>
				</NInput>
			</div>),
			width: 235,
			render: (rowData) => {
				return (<NSpace size="medium">
					<NButton type="primary" size="small" onClick={() => update(rowData.id)}>修改</NButton>
					<NButton type="error" size="small" onClick={() => del(rowData.id)}>删除</NButton>
				</NSpace>)
			}
		}
	]
}
const add = (data) => {
	if (!data.classId) {
		message.error('请先选择班级')
		return
	}
	data.showEditProps.id = null
	data.showEditProps.title = '添加管理员'
	data.showEditProps.show = true
}
const upDateTable = async (params, tableData) => {
	tableData.value = await api.getTableData(params)
}
const createFormData = async (id, classId) => { 
	const formData = {
		studentNo: '',
		name: '',
		sex: '',
		phone: '',
		email: '',
		idcard: '',
		year: null,
		inSchool: '',
		className: '',
		classId: null,
		classNo: ''
	}
	if (id) {
		formData.id = id
		const content = await api.getById(id)
		copyValue(formData, content)
	} else {
		formData.classId = classId
	}
	return formData
}
const createRules = (id) => {
	return {
		studentNo: [
			{ required: true, message: '请输入学号', trigger: 'blur' },
			{ max: 20, message: '学号最多20个字符', trigger: 'blur' },
			{ validator:(field, value, callback) => api.check('student_no', value, callback, id), message: '该学号已经存在', trigger: 'blur'}
		],
		name: [
			{ required: true, message: '请输入姓名', trigger: 'blur' },
			{ max: 20, message: '姓名最多20个字符', trigger: 'blur' }
		],
		sex: [
			{ required: true, message: '请选择性别', trigger: 'blur' }
		],
		phone: [
			{ pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' },
			{ validator:(field, value, callback) => api.check('phone', value, callback, id), message: '该手机号已经使用', trigger: 'blur'}
		],
		email: [
			{ type: 'email', message: '请输入正确的电子信箱', trigger: 'blur' },
			{ validator: (field, value, callback) => api.check('email', value, callback, id), message: '该电子信箱已经使用', trigger: 'blur' }
		],
		idcard: [
			{ pattern: /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[0-9Xx]$/, message: '请输入正确的身份证号', trigger: 'blur' },
			{ validator: (field, value, callback) => api.check('idcard', value, callback, id), message: '该身份证号已经使用', trigger: 'blur' }
		],
		year: [
			{ required: true, message: '请选择入学年份', trigger: 'blur' }
		],
		className: [
			{ required: true, message: '请输入所属末级组织名称', trigger: 'blur' },
			{ max: 64, message: '所属末级组织名称最多64个字符', trigger: 'blur' }
		],
		classNo: [
			{ required: true, message: '请输入所属末级组织代码', trigger: 'blur' },
			{ max: 64, message: '所属末级组织代码最多64个字符', trigger: 'blur' }
		]
	}
}
const changeFile = (file, showImportRef) => {
	api.uploadFile(file, content => {
		showImportRef.tableData = content
		showImportRef.value = false
	})
}
const createImportColumns = () => {
	return [
		{
			title: '学号',
			key: 'studentNo'
		},
		{
			title: '姓名',
			key: 'name'
		},
		{
			title: '性别',
			key: 'sex'
		},
		{
			title: '手机号',
			key: 'phone'
		},
		{
			title: '电子信箱',
			key: 'email'
		},
		{
			title: '身份证号',
			key: 'idcard'
		},
		{
			title: '入学年份',
			key: 'year'
		},
		{
			title: '是否在校/岗',
			key: 'inSchool'
		},
		{
			title: '所属末级组织名称',
			key: 'className'
		},
		{
			title: '所属末级组织代码',
			key: 'classNo'
		}
	]
}
const addStudents = ({content, classId}, showImportRef) => [
	api.addStudents({content, classId}, content => {
		if (content) {
			showImportRef.tableData = []
		}
	})
]

export default {
	add,
	getTableData: api.getTableData,
	createTableColumns,
	upDateTable,
	createFormData,
	createRules,
	addOrUpdate: api.addOrUpdate,
	getGrades: gradeApi.getList,
	changeFile,
	createImportColumns,
	addStudents
}